package com.cash.app.ui.auth

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.cash.app.R
import com.cash.app.base.BaseActivity
import com.cash.app.constant.AppConstant
import com.cash.app.constant.AppConstant.AUTH_O_REQUEST_OPTION
import com.cash.app.custom.AppActionBar
import com.cash.app.databinding.AcAuthPersonalOptionBinding
import com.cash.app.manager.AuthMetaDataManager
import com.cash.app.ui.auth.adapter.AuthOptionAdapter
import com.cash.app.ui.auth.model.AuthOptionItem
import com.cash.app.utils.addVerticalSpacing

class AuthPersonalOptionActivity : BaseActivity() {

    companion object {
        private const val EXTRA_OPTION_TYPE = "OPTION_TYPE"
        const val EXTRA_SELECTED_ID = "SELECTED_ID"
        const val EXTRA_SELECTED_VALUE = "SELECTED_VALUE"

        fun start(context: Context, optionType: String) {
            val intent = Intent(context, AuthPersonalOptionActivity::class.java).apply {
                putExtra(EXTRA_OPTION_TYPE, optionType)
            }
            (context as Activity).startActivityForResult(intent, AUTH_O_REQUEST_OPTION)
        }
    }

    private lateinit var binding: AcAuthPersonalOptionBinding
    private val adapter = AuthOptionAdapter()
    private var optionType = ""

    override fun setViewBind() {
        binding = AcAuthPersonalOptionBinding.inflate(layoutInflater)
    }

    override fun getLayoutView(): View = binding.root

    override fun getAppActionBar(): AppActionBar = binding.optionActionBar

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        optionType = intent.getStringExtra(EXTRA_OPTION_TYPE) ?: ""
        setupRecyclerView()
        setupActionBar()
        loadOptionData()
    }

    private fun setupRecyclerView() {
        binding.optionListRv.apply {
            layoutManager = LinearLayoutManager(this@AuthPersonalOptionActivity)
            adapter = this@AuthPersonalOptionActivity.adapter
            addVerticalSpacing(resources.getDimensionPixelSize(R.dimen.item_1dp))
        }
    }

    private fun setupActionBar() {
        if (optionType.isNotEmpty()) {
            binding.optionActionBar.setTitle(optionType)
        }

        binding.optionActionBar.setRightTextClickListener {
            adapter.getSelectedItem()?.let { item ->
                setResult(Activity.RESULT_OK, Intent().apply {
                    putExtra(EXTRA_SELECTED_ID, item.id)
                    putExtra(EXTRA_SELECTED_VALUE, item.name)
                })
                finish()
            } ?: Toast.makeText(this, getString(R.string.auth_p_select_hint), Toast.LENGTH_SHORT)
                .show()
        }
    }

    private fun loadOptionData() {
        val metaData = AuthMetaDataManager.getMetaData()

        val options = when (optionType) {
            AppConstant.AUTH_O_EDUCATION -> metaData?.education?.map { (key, value) ->
                AuthOptionItem(key, value)
            } ?: emptyList()

            AppConstant.AUTH_O_MARITAL -> metaData?.marriage?.map { (key, value) ->
                AuthOptionItem(key, value)
            } ?: emptyList()

            AppConstant.AUTH_O_CHILDREN -> metaData?.child?.map { (key, value) ->
                AuthOptionItem(key, value)
            } ?: emptyList()

            AppConstant.AUTH_O_CAREER -> metaData?.industryType?.map { (key, value) ->
                AuthOptionItem(key, value)
            } ?: emptyList()

            AppConstant.AUTH_O_INCOME -> metaData?.monthIncome?.map { (key, value) ->
                AuthOptionItem(key, value)
            } ?: emptyList()

            AppConstant.AUTH_O_FIRST_CONTACT -> metaData?.emergency1?.map { (key, value) ->
                AuthOptionItem(key, value)
            } ?: emptyList()

            AppConstant.AUTH_O_SECOND_CONTACT -> metaData?.emergency2?.map { (key, value) ->
                AuthOptionItem(key, value)
            } ?: emptyList()

            else -> emptyList()
        }
        adapter.setData(options)
    }
} 